﻿<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
    <script type="text/javascript" src="../../Content/highlighter/scripts/shCore.js"></script>
    <script type="text/javascript" src="../../Content/highlighter/scripts/shBrushCSharp.js"></script>
    <script type="text/javascript" src="../../Content/highlighter/scripts/shBrushXml.js"></script>
    <link type="text/css" rel="stylesheet" href="../../Content/highlighter/styles/shCore.css" />
    <link type="text/css" rel="stylesheet" href="../../Content/highlighter/styles/shCoreEclipse.css" />
    <script type="text/javascript">SyntaxHighlighter.all();</script>
</head>
<body>
    <h3>Dapper ORM</h3>
    <a href="https://github.com/StackExchange/Dapper">Dapper开源主页</a><br />
    <b>
        1.Execute a query and map the results to a strongly typed List
    </b>
    <pre class="brush:csharp;">
        public static IEnumerable<T> Query<T>(this IDbConnection cnn, string sql, 
            object param = null, SqlTransaction transaction = null, bool buffered = true)
    </pre>
    <p>
        Example usage:<br />
        <pre class="brush:csharp;">
            public class Dog
            {
                public int? Age { get; set; }
                public Guid Id { get; set; }
                public string Name { get; set; }
                public float? Weight { get; set; }

                public int IgnoredProperty { get { return 1; } }
            }            
            
            var guid = Guid.NewGuid();
            var dog = connection.Query&lt;Dog>("select Age = @Age, Id = @Id", new { Age = (int?)null, Id = guid });
            
            dog.Count()
                .IsEqualTo(1);

            dog.First().Age
                .IsNull();

            dog.First().Id
                .IsEqualTo(guid);
        </pre>
    </p>
    <b>2.Execute a query and map it to a list of dynamic objects</b>
    <pre class="brush:csharp;">
        public static IEnumerable<dynamic> Query (this IDbConnection cnn, 
            string sql, object param = null, SqlTransaction transaction = null, bool buffered = true)
    </pre>
    <p>
        This method will execute SQL and return a dynamic list.<br/>
        Example usage:<br />
        <pre class="brush:csharp;">
            var rows = connection.Query("select 1 A, 2 B union all select 3, 4");

            ((int)rows[0].A)
               .IsEqualTo(1);

            ((int)rows[0].B)
               .IsEqualTo(2);

            ((int)rows[1].A)
               .IsEqualTo(3);

            ((int)rows[1].B)
                .IsEqualTo(4);
        </pre>
    </p>
    <b>3.Execute a Command that returns no results</b>
    <pre class="brush:csharp;">
        public static int Execute(this IDbConnection cnn, 
            string sql, object param = null, SqlTransaction transaction = null)
    </pre>
    <p>
        Example usage:<br/>
        <pre class="brush:csharp;">
            connection.Execute(@"
                set nocount on 
                create table #t(i int) 
                set nocount off 
                insert #t 
                select @a a union all select @b 
                set nocount on 
                drop table #t", new {a=1, b=2 })
                .IsEqualTo(2);
        </pre>
    </p>
    <b>4.Execute a Command multiple times</b>
    <p>
        The same signature also allows you to conveniently and efficiently execute<br/>
        a command multiple times (for example to bulk-load data)<br />
    </p>
    <p>
        Example usage:<br />
        <pre class="brush:csharp;">
            connection.Execute(@"insert MyTable(colA, colB) values (@a, @b)",
                new[] { new { a=1, b=1 }, new { a=2, b=2 }, new { a=3, b=3 } }
                ).IsEqualTo(3); // 3 rows inserted: "1,1", "2,2" and "3,3"
        </pre>
        This works for any parameter that implements IEnumerable for some T.
    </p>
</body>
</html>